package com.woniu.shops.config;

/**
 * Author 史豪通
 * Date 2024/09/12 23:29
 */

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOrigin("http://www.flowers.com"); // 允许的源
        config.addAllowedMethod("*"); // 允许的 HTTP 方法，这里设置为所有
        config.addAllowedHeader("*"); // 允许的请求头，这里设置为所有
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}
